package cn.jiangxiong.pethome.system.user.controller;

import cn.jiangxiong.pethome.system.user.domain.Employee;
import cn.jiangxiong.pethome.system.user.domain.EmployeeRo;
import cn.jiangxiong.pethome.system.user.query.EmployeeQuery;
import cn.jiangxiong.pethome.system.user.service.IEmployeeService;
import cn.jiangxiong.pethome.utils.AjaxResult;
import cn.jiangxiong.pethome.utils.BusinessException;
import cn.jiangxiong.pethome.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {
    @Autowired
    private IEmployeeService iEmployeeService;

    @GetMapping("/list")
    @ApiOperation(value = "获取所有的员工信息" )
    public List<Employee> findAll(){
        return iEmployeeService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询员工信息")
    public AjaxResult findOne(@PathVariable Long id){
        try {
            Employee one = iEmployeeService.findOne(id);
            return AjaxResult.success().setData(one);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @PostMapping("/pagelist")
    @ApiOperation(value = "员工列表分页查询")
    public AjaxResult findQuery(@RequestBody EmployeeQuery query){
        try {
            List<Employee> byQuery = iEmployeeService.findByQuery(query);
            Long count = iEmployeeService.findByQueryCount(query);
            return  AjaxResult.success().setData(byQuery).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据员工id删除数据")
    public AjaxResult delete(@PathVariable Long id){
        try {
            iEmployeeService.delete(id);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }

    }
    @PostMapping
    @ApiOperation(value = "保存员工信息")
    public AjaxResult save(@RequestBody Employee employee){
        try {
            if (employee.getId() != null){
                iEmployeeService.update(employee);

            }else {
                iEmployeeService.add(employee);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
    @PatchMapping("/del")
    @ApiOperation(value = "根据id批量删除数据")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            iEmployeeService.batchDel(ids);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("删除失败");
    }


    @GetMapping("/export")
    public void export(HttpServletResponse response){
        List<Employee> all = iEmployeeService.findAll();
        ExcelUtil.exportExcel(all,"员工数据表","员工数据", Employee.class,"导出的员工数据.xls",response);
    }


    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Employee> employees = ExcelUtil.importExcel(file, 1, 1, Employee.class);
        employees.forEach(System.out::println);
    }

    @GetMapping("/countByDate")
    public AjaxResult countByDate(){
        try {
            List<EmployeeRo> employeeRos = iEmployeeService.countByDate();
            return AjaxResult.success().setData(employeeRos);
        }catch (BusinessException businessException){
            return AjaxResult.error().setMsg(businessException.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg("网络异常，请稍后再试！");
        }
    }
}
